/*
 * QuestionsFilterJPanel.java
 *
 * Created on 24.01.2009, 12:55:38
 */
package morenoapp.components.thinktable;

import java.awt.Container;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import morenoapp.MorenoApp;
import morenoapp.components.common.MorenoFilteredTableModel;
import morenoapp.components.common.QuestionsFilter;
import morenoapp.components.help.MorenoHelp;
import morenoapp.model.enums.MorenoQuestionType;
import morenoapp.model.data.MorenoQuestionFace;
import morenoapp.model.listeners.MorenoModelAdapter;
import morenoapp.util.I18N;
import morenoapp.util.MorenoUtil;

/**
 *
 * @author root
 */
public class ThinktableQuestionsFilterJPanel extends javax.swing.JPanel implements QuestionsFilter, ChangeListener {

    private List<JCheckBox> filterCheckBoxesList = new ArrayList<JCheckBox>();
    private List<MorenoQuestionFace> filteredQuestions = new ArrayList<MorenoQuestionFace>();
    private MorenoFilteredTableModel filteredTableModel = null;
    private boolean passListenersNotification = false;

    /** Creates new form QuestionsFilterJPanel */
    public ThinktableQuestionsFilterJPanel() {
        initComponents();
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        applyNewLayout();
        MorenoModelAdapter modelListener = new MorenoModelAdapter() {

            @Override
            public void onQuestionChanged(MorenoQuestionFace queston) {
                //TODO Изменять метку на соответстующем компоненте.
            }

            @Override
            public void onQuestionsListChanged() {
                applyNewLayout();
            }

            @Override
            public void onWholeModelChanged() {
                applyNewLayout();
            }
        };
        MorenoApp.getApplicationData().getMorenoModelProxy().addModelListener(modelListener);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setName("Form"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 104, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 23, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    public MorenoFilteredTableModel getFilteredTableModel() {
        return filteredTableModel;
    }

    public void setFilteredTableModel(MorenoFilteredTableModel abstractMorenoRespondentsTableModel) {
        this.filteredTableModel = abstractMorenoRespondentsTableModel;
    }

    private void applyNewLayout() {
        removeAll();
        filteredQuestions.clear();
        filterCheckBoxesList.clear();

        for (MorenoQuestionFace question : MorenoApp.getApplicationData().getMorenoModelProxy().getQuestionsList()) {
            if (filterCheckBoxesList.size() > 0) {
                add(new JSeparator(JSeparator.VERTICAL));
            }
            JCheckBox checkBox = new JCheckBox();
            checkBox.setText("<html>" + MorenoUtil.wrapTextWithColor(question.getColor(), "\u2588") + " " + question.getNumber() + ") <b>" + //NOI18N
                    MorenoUtil.wrapTextWithColor(question.getColor(), question.getMarker()) + "</b> "); //NOI18N
            checkBox.setToolTipText("<HTML>"+I18N.get("label.show_poll_results_by_question")+":<br>" +
                    "<b>" + //NOI18N
                    MorenoUtil.wrapTextWithColor(question.getColor(), question.getContent()) +
                    "</b><br/><br/>" + //NOI18N
                    I18N.get("label.question.type")+": " + question.getType().getHtmlDescription());
            checkBox.setModel(new QuestionToggleButtonModel(question));
            checkBox.setSelected(true);
            filteredQuestions.add(question);
            checkBox.addChangeListener(this);

            add(checkBox);
            filterCheckBoxesList.add(checkBox);
        }
        if (filterCheckBoxesList.size() == 0) {
            add(new JLabel(I18N.get("label.question.no_questions")));
        }
        add(new JSeparator(JSeparator.VERTICAL));
        add(MorenoHelp.createHelpToolbar("sociomatrix"));
        Container container = getParent() != null ? getParent() : this;
        container.invalidate();
        container.validate();
        stateChanged(null); //apply current state
    }

    public List<JCheckBox> getFilterCheckBoxesList() {
        return Collections.unmodifiableList(filterCheckBoxesList);
    }

    @Override
    public List<MorenoQuestionFace> getFilteredQuestionList() {
        return Collections.unmodifiableList(filteredQuestions);
    }

    public void applyFilterByQuestionType(MorenoQuestionType _questionType) {
        passListenersNotification = true;
        try {
            for (JCheckBox box : filterCheckBoxesList) {
                QuestionToggleButtonModel boxModel = (QuestionToggleButtonModel) box.getModel();
                box.setSelected(_questionType==null || _questionType == boxModel.getQuestion().getType());
            }
        } finally {
            passListenersNotification = false;
            stateChanged(null);
        }
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        if (passListenersNotification) {
            return;
        }

        filteredQuestions.clear();
        for (JCheckBox box : filterCheckBoxesList) {
            if (box.isSelected()) {
                filteredQuestions.add(((QuestionToggleButtonModel) box.getModel()).getQuestion());
            }
        }
        if (filteredTableModel != null) {
            filteredTableModel.fireFilterChanged();
        }
    }

    public static class QuestionToggleButtonModel extends javax.swing.JToggleButton.ToggleButtonModel {

        private MorenoQuestionFace question;

        public QuestionToggleButtonModel(MorenoQuestionFace question) {
            super();
            this.question = question;
        }

        public MorenoQuestionFace getQuestion() {
            return question;
        }
    }
}
